/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Mozilla Communicator client code.
 *
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1998
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Kohei TAKETA <k-tak@void.in> (Java port)
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

package org.mozilla.universalchardet.prober.contextanalysis;

public abstract class JapaneseContextAnalysis
{
    ////////////////////////////////////////////////////////////////
    // constants
    ////////////////////////////////////////////////////////////////
    public static final int NUM_OF_CATEGORY         = 6;
    public static final int ENOUGH_REL_THRESHOLD    = 100;
    public static final int MAX_REL_THRESHOLD       = 1000;
    public static final int MINIMUM_DATA_THRESHOLD  = 4;
    public static final float DONT_KNOW             = -1f;

    
    ////////////////////////////////////////////////////////////////
    // inner types
    ////////////////////////////////////////////////////////////////
    protected class Order
    {
        public int order;
        public int charLength;
        
        public Order()
        {
            this.order = -1;
            this.charLength = 0;
        }
    }
    
    ////////////////////////////////////////////////////////////////
    // fields
    ////////////////////////////////////////////////////////////////
    private int[]   relSample = new int[NUM_OF_CATEGORY];
    private int     totalRel;
    private int     lastCharOrder;
    private int     needToSkipCharNum;
    private boolean done;
    
    private Order   tmpOrder;
    
    
    ////////////////////////////////////////////////////////////////
    // methods
    ////////////////////////////////////////////////////////////////
    public JapaneseContextAnalysis()
    {
        tmpOrder = new Order();
        reset();
    }
    
    public void handleData(final byte[] buf, int offset, int length)
    {
        if (this.done) {
            return;
        }
        
        // The buffer we got is byte oriented, and a character may span in more than one
        // buffers. In case the last one or two byte in last buffer is not complete, we 
        // record how many byte needed to complete that character and skip these bytes here.
        // We can choose to record those bytes as well and analyse the character once it 
        // is complete, but since a character will not make much difference, by simply skipping
        // this character will simply our logic and improve performance.
        int maxPos = offset + length;

        for (int i=this.needToSkipCharNum+offset; i<maxPos; ) {
            getOrder(this.tmpOrder, buf, i);
            i += this.tmpOrder.charLength;
            
            if (i > maxPos) {
                this.needToSkipCharNum = i - maxPos;
                this.lastCharOrder = -1;
            } else {
                if (this.tmpOrder.order != -1 && this.lastCharOrder != -1) {
                    ++this.totalRel;
                    if (this.totalRel > MAX_REL_THRESHOLD) {
                        this.done = true;
                        break;
                    }
                    ++this.relSample[jp2CharContext[this.lastCharOrder][this.tmpOrder.order]];
                }
            }
        }
    }
    
    public void handleOneChar(final byte[] buf, int offset, int charLength)
    {
        if (this.totalRel > MAX_REL_THRESHOLD) {
            this.done = true;
        }
        if (this.done) {
            return;
        }

        
        int orderNum = -1;
        if (charLength == 2) {
            orderNum = getOrder(buf, offset);
        }
        if (orderNum != -1 && this.lastCharOrder != -1) {
            ++this.totalRel;
            ++this.relSample[jp2CharContext[this.lastCharOrder][orderNum]];
        }
        this.lastCharOrder = orderNum;
    }

    public float getConfidence()
    {
        if (this.totalRel > MINIMUM_DATA_THRESHOLD) {
            return ((float)(this.totalRel - this.relSample[0])) / this.totalRel;
        } else {
            return DONT_KNOW;
        }
    }
    
    public void reset()
    {
        this.totalRel = 0;
        for (int i=0; i<NUM_OF_CATEGORY; ++i) {
            this.relSample[i] = 0;
        }
        this.needToSkipCharNum = 0;
        this.lastCharOrder = -1;
        this.done = false;
    }
    
    public void setOption()
    {}
    
    public boolean gotEnoughData()
    {
        return (this.totalRel > ENOUGH_REL_THRESHOLD);
    }
    
    protected abstract void getOrder(Order order, final byte[] buf, int offset);
    protected abstract int getOrder(final byte[] buf, int offset);

    ////////////////////////////////////////////////////////////////
    // constants continued
    ////////////////////////////////////////////////////////////////
    protected static final byte[][] jp2CharContext = new byte[][] {
        { 0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,},
        { 2,4,0,4,0,3,0,4,0,3,4,4,4,2,4,3,3,4,3,2,3,3,4,2,3,3,3,2,4,1,4,3,3,1,5,4,3,4,3,4,3,5,3,0,3,5,4,2,0,3,1,0,3,3,0,3,3,0,1,1,0,4,3,0,3,3,0,4,0,2,0,3,5,5,5,5,4,0,4,1,0,3,4,},
        { 0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,},
        { 0,4,0,5,0,5,0,4,0,4,5,4,4,3,5,3,5,1,5,3,4,3,4,4,3,4,3,3,4,3,5,4,4,3,5,5,3,5,5,5,3,5,5,3,4,5,5,3,1,3,2,0,3,4,0,4,2,0,4,2,1,5,3,2,3,5,0,4,0,2,0,5,4,4,5,4,5,0,4,0,0,4,4,},
        { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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